import axios from "../../utils/axios"
import dayjs from "dayjs"
Component({
  options: {
    addGlobalClass: true,
  },
  data: {
    list:[],
    active:"2"
  },
  attached(){
    this.getList();
  },
  methods:{
    onTabsChange(event) {
      this.getList(event.detail.value)
      this.setData({
        active:event.detail.value
      })
    },

    onTabsClick(event) {
      // console.log(`Click tab, tab-panel value is ${event.detail.value}.`);
    },

    toPhone(e){
      wx.makePhoneCall({
        phoneNumber: e.currentTarget.dataset.u
      })
    },
    getList(status){
      this.setData({
        list:[]
      })
      wx.showLoading({
        title: '加载中...',
      })
      axios.get("/app/orderList",{
        current:1,
        pageSize:999,
        status:status || this.data.active,
        userId:wx.getStorageSync('company_id'),
        type:wx.getStorageSync('role') === "茶艺师"?4:1
      }).then(res=>{
        wx.hideLoading();
        if(res.data?.[0]?.status !== this.data.active*1){
          // this.setData({
          //   list:[]
          // })
          return;
        }
        this.setData({
          list:res.data.map(item=>{
            const {phone,remark,dpName} = JSON.parse(item.remark ||"{}")
            return {
              ...item,
              phone:item.status==5?"******":phone,
              remark,
              dpName,
              timer:dayjs(item.startTime).format("YYYY-MM-DD HH:mm")+" - "+ dayjs(item.endTime).format("HH:mm"),
              showContact:item.status!=5,
              statusText:this.getStatus(item.status)
            }
          })
        })
      })
    },
    changeOrderStatus(e){
      const {status,orderid} = e.currentTarget.dataset;
      axios.post("/app/changeOrderStatus",{
          id:orderid,
          status
      }).then(res=>{
        let title="";
        if(status === 3){
          title="接单成功！"
        }
        if(status === 4){
          title="开始工作！"
        }
        if(status === 5){
          title="工作结束！"
        }
          wx.showToast({
            title,
          })
          this.getList(this.data.active)
      })
  },
   getStatus(status){
        if (status === 2){
            return "待接单"
        }
        if (status === 3){
            return "待到店"
        }
        if (status === 4){
            return "服务中"
        }
        if (status === 5){
            return "服务完成"
        }
    }
  },
  lifetimes:{

  }
})